<?php
/* -=-=-=-=-=-=-=-=-=-=-=-=-
     author by terry
-=-=-=-=-=-=-=-=-=-=-=-=- */
error_reporting(7);
require "global.php";
cpheader();

if ($_POST[action]=="update"){
    $starter_resume_view   =  htmlspecialchars(trim($_POST['starter_resume_view']));
    $recruiter_resume_view   =  htmlspecialchars(trim($_POST['recruiter_resume_view']));

    $sql = "UPDATE ".$db_prefix."settings SET  `value` = '".$starter_resume_view."' WHERE `key`='starter_resume_view' ";
	$DB->query($sql);

    $sql = "UPDATE ".$db_prefix."settings SET  `value` = '".$recruiter_resume_view."' WHERE `key`='recruiter_resume_view' ";
	$DB->query($sql);

    redirect("./settings.php","<font color=red>Modified Successfully!</font>");
	exit;
}

?>
<?
$sql = "SELECT * FROM pa_settings";
$tmpSettings = $DB->fetch_all($sql);
if(!empty($tmpSettings)){
	foreach($tmpSettings as $set){
		$settings[$set['key']] = $set['value'];
	}
}

$cpforms->formheader(array('title'=>'Settings (<font color=red>*</font>=Required Field)', 'name'=>'article'));
$cpforms->makehidden(array('name'=>'action','value'=>'update'));
$cpforms->gethightext(array('text'=>"The Total Resumes Starter Users Can View Per Month",'name'=>"starter_resume_view",'value'=>"$settings[starter_resume_view]",'view'=>"$view"));
$cpforms->gethightext(array('text'=>"The Total Resumes Director Users Can View Per Month",'name'=>"recruiter_resume_view",'value'=>"$settings[recruiter_resume_view]",'view'=>"$view"));

?>

<?

   if($_GET[action]=="view"){$cpforms->noformfooter();}
	   else{ $cpforms->formfooter(array('confirm'=>1));}
?>
<br /><br />

<?
if($_REQUEST[action] == "resorting"){
	$imgurlArr = json_decode($settings['slide_show'],true);

	$sortArr = explode("|",$_POST['Sortorder']);
		
	if(is_array($sortArr) && count($sortArr)>0){
		foreach($sortArr as $v){
			if(isset($imgurlArr[$v])){
				$newurlArr[$v] = $imgurlArr[$v];
			}
		}
	}
	$imgurlInSQL = json_encode($newurlArr);
	$query = $DB->query("UPDATE pa_settings SET `value`='{$imgurlInSQL}' WHERE `key`='slide_show'");
	exit;
}
if($_POST[action] == "upload" && !empty($_FILES)){
		for($k=1;$k<15;$k++){
				$b_key = 'ff_'.$k."_0";
				$titlekey = 'ff_'.$k."_1";
				$textkey = 'ff_'.$k."_2";
				$isopen = 0;
				if($_FILES[$b_key]['name'] != ''){
					$f = new upfile("../userfiles/image/","gif,jpg,png");
					if($f->upload($b_key)){
						$fileField[$k]['bigImg']  = $f->savename;
						$isopen = 1;
					}
				}
				if($isopen == 1){
					$fileField[$k]['title'] = $_POST[$titlekey];
					$fileField[$k]['text'] = $_POST[$textkey];
				}
		}

		$imgurlArr = json_decode($settings['slide_show'],true);
		if(!(is_array($imgurlArr) && count($imgurlArr)>0)){
			$imgurlArr = array();
		}	
		$newImgArr = @array_merge($fileField,$imgurlArr);
		$i=1;
		if(count($newImgArr) > 0) {
			foreach($newImgArr as $v){
				$imgArr[$i] = $v;
				$i++;
			}
			$imgurlInSQL = json_encode($imgArr);
			$DB->query("UPDATE pa_settings SET `value`='$imgurlInSQL' WHERE `key`='slide_show'");
		}

		//reload the data
		$sql = "SELECT * FROM pa_settings";
		$tmpSettings = $DB->fetch_all($sql);
		if(!empty($tmpSettings)){
			foreach($tmpSettings as $set){
				$settings[$set['key']] = $set['value'];
			}
		}
}

if($_GET[action] == "deleteImg"){
		$key = $_GET['key'];
		$imgurlArr = json_decode($settings['slide_show'],true);

		unset($imgurlArr[$key]);
		@unlink("../userfiles/image/".$imgurlArr[$key]['bigImg']);
		@unlink("../userfiles/image/".$imgurlArr[$key]['smallImg']);
		$imgurlInSQL = json_encode($imgurlArr);		

		$query = $DB->query("UPDATE pa_settings SET `value`='$imgurlInSQL' WHERE `key`='slide_show' ");

		redirect("./settings.php?action=edit&c_id=1", "<span style=\"color: red\">Removed Successfully!</span>");
		exit;

}
?>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<link rel="stylesheet" media="screen,projection" type="text/css" href="../css/ui-lightness/jquery-ui-1.7.3.custom.css" />
<script type="text/javascript" src="../js/jquery-ui-1.7.3.custom.min.js"></script>
<script>
	var i = 0;
	function addEle(){
	i++;
	if(i>14){
		alert("too much....");
		return false;
	}
	var jQ_file = $('<div>Logo image: <input type="file" />  Title: <input type="text" /> Link:  <input type="text" /><br /></div>');
	jQ_file.children().each(function(j){
		$(this).attr({
			id: "ff_"+i+"_"+j,
			name: "ff_"+i+"_"+j
		});
	});
	$("#aa").append(jQ_file);
	return false;
	}

	$(function(){
		addEle();
	});
</script>
<style>
	#content #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
	#content #sortable li { background:url("images/doublearrow.png") no-repeat scroll 100% 50% transparent; margin: 0 5px 5px 5px; padding: 5px; height: auto; border:1px solid #3366CC; }
	#content #sortable img { width:100px;} 
	.fullscreen {width:100%}
	.ui-state-highlight { height: 1.5em; line-height: 1.2em; }
	.clear { clear:both; }
</style>
<script type="text/javascript">
	$(function() {
		$( "#sortable" ).sortable({
			update : function(){
				//var serial = $("#sortable").sortable('toArray');
				var strs = '';
				$("#sortable img").each(function(){
					strs += $(this).attr("title") + "|";
				});
				$.ajax({
					url: './settings.php?action=resorting',
					type:'POST',
					data:{Sortorder:strs},
					success: function(msg){
						alert("Sorting successfully.");
					}
				});
			}
		});
		$( "#sortable" ).disableSelection();
	});

</script>
<?
$cpforms->formheader(array('title'=>'Upload/change logo images', 'name'=>'article', 'enctype'=>'multipart/form-data'));
$cpforms->makehidden(array('name'=>'action','value'=>'upload'));
?>

		<tr>
			<td>
			<div style="width:500px;">
			You can sort the logo images through drag&drop. Logo image (height < 90):
				<ul id="sortable" style="list-style-type:none;padding:0; margin:0">
				<?
					$imgurlArr = json_decode($settings["slide_show"],true);
					if(is_array($imgurlArr) && count($imgurlArr)>0){
					$i = 1;
					foreach($imgurlArr as $k => $img){
				?>
					<li class="ui-state-default">
					Logo image <?=$i?>: <img src="../userfiles/image/<?=$img['bigImg']?>" title="<?echo $k;?>"/>
					<br />
					Title <?=$i?>: <?=$img['title']?> 
					<br /> 
					Link <?=$i?>: <?=$img['text']?>  
					.......................................<a href="./settings.php?action=deleteImg&key=<?=$k?>">[Delete]</a>
					<div class="clear"></div>
					</li>
				<?
					$i ++;
					}
				}
				?>
				</ul>
			</div>
				<div id="aa">
						
				</div>
				 <input type="button" onclick="return addEle()" value="Add new" />
			</td>
		</tr>

<?

if($_GET[action]=="view"){$cpforms->noformfooter();}
	   else{ $cpforms->formfooter(array('confirm'=>1));}
cpfooter();
?>